<?php

use Illuminate\Http\Request;
use SwooleTW\Http\Websocket\Facades\Websocket;

/*
|--------------------------------------------------------------------------
| Websocket Routes
|--------------------------------------------------------------------------
|
| Here is where you can register websocket events for your application.
|
*/

Websocket::on('connect', function ($websocket, Request $request) {
    // called while socket on connect

    $user_id = $request->get('user_id');

    $user = \App\Models\User::find($user_id);

    \Cache::put('fd:' . $user->id, $websocket->getSender(), 3600 * 24 * 2);#存放资源标记

    $websocket->loginUsing($user);#登录并创建房间

    $websocket->emit('sys_message', 'connect success !');

});

Websocket::on('disconnect', function ($websocket) {
    // called while socket on disconnect
    $websocket->emit('sys_message', 'good bye');
});

Websocket::on('request', function ($websocket, Request $request) {
    $websocket->emit('sys_message', 'request is ' . json_encode([$request->all()]));
});
